/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.box.ctrl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pitop.box.entity.Box;
import com.pitop.box.entity.BoxState;
import com.pitop.box.service.BoxSVImpl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 箱库存 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/box")
@Slf4j
@Api(value = "箱库存控制器", description = "箱库存控制器")
public class BoxCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private BoxSVImpl boxSVImpl;


    /**
     * 查询Box一个详情信息
     *
     * @param id
     * @param code 编码
     * @param sn   箱SN
     * @return BeanRet
     */
    @ApiOperation(value = "查询Box一个详情信息", notes = "查询Box一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱SN", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code, String sn) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        if (sn == null) {
            return null;
        }
        Box box = boxSVImpl.load(id, code, sn);
        log.info(JSON.toJSONString(box));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }


    /**
     * 根据条件id查询Box一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Box一个详情信息", notes = "查询Box一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Box box = boxSVImpl.loadById(id);
        log.info(JSON.toJSONString(box));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }

    /**
     * 根据条件code查询Box一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Box一个详情信息", notes = "查询Box一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Box box = boxSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(box));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }

    /**
     * 根据条件sn查询Box一个详情信息
     *
     * @param sn 箱SN
     * @return BeanRet
     */
    @ApiOperation(value = "查询Box一个详情信息", notes = "查询Box一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "箱SN", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/sn/{sn}")
    @ResponseBody
    public BeanRet loadBySn(@PathVariable String sn) {
        if (StringUtils.isEmpty(sn)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Box box = boxSVImpl.loadBySn(sn);
        log.info(JSON.toJSONString(box));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }


    /**
     * 查询Box信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Box信息集合", notes = "查询Box信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "state", value = "箱库存状态", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱库存sn码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Box box, @ApiIgnore Page<Box> page, BoxState state) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (state != null) {
            box.setState(state.name());
        }
        int total = 0;
        List<Box> boxs = boxSVImpl.list(box, page.getCurPage(), page.getPageSize());
        for (Box boxFlag : boxs) {
            if (boxFlag.getBoxItems() != null && boxFlag.getBoxItems().size() > 0) {
                total += boxFlag.getBoxItems().size();
            }
        }
        page.setTotalRow(boxSVImpl.count(box));
        page.setVoList(boxs);
        log.info(JSON.toJSONString(page));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", page);
        jsonObject.put("curPageItemNum", total);
        int allTotal = boxSVImpl.countByBox(box.getSn(), box.getState(), box.getFactoryCode());
        jsonObject.put("totalBoxItemNum", allTotal);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "查出成功", jsonObject);
    }


    /**
     * 查询Box信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Box信息集合", notes = "查询Box信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱SN", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, String sn, @ApiIgnore Page<Box> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Box> boxs = boxSVImpl.list(id, code, sn, page.getCurPage(), page.getPageSize());
        int total = boxSVImpl.count(id, code, sn);
        page.setTotalRow(total);
        page.setVoList(boxs);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建Box
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Box", notes = "创建Box")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱SN", paramType = "query"),
            @ApiImplicitParam(name = "jobNumber", value = "员工号", paramType = "query"),
            @ApiImplicitParam(name = "syncState", value = "同步状态：准备同步 Ready,同步中 Sync,同步错误 Error,已经同步 Synced", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：入库 Storage，已出库 Issue", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Box box) {
        if (StringUtils.isEmpty(box.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getJobNumber())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getSyncState())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getState())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        boxSVImpl.save(box);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }


    /**
     * 修改Box
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Box", notes = "修改Box")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱SN", paramType = "query"),
            @ApiImplicitParam(name = "jobNumber", value = "员工号", paramType = "query"),
            @ApiImplicitParam(name = "syncState", value = "同步状态：准备同步 Ready,同步中 Sync,同步错误 Error,已经同步 Synced", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：入库 Storage，已出库 Issue", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Box box) {
        if (StringUtils.isEmpty(box.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getJobNumber())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getSyncState())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(box.getState())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        boxSVImpl.modify(box);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, box);
    }

    /**
     * 根据主键删除Box
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Box", notes = "删除Box")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "箱SN", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code, String sn) {
        if (id != null && StringUtils.isEmpty(code) && StringUtils.isEmpty(sn)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        boxSVImpl.delete(id, code, sn);
        return BeanRet.create(true, "删除Box成功");
    }


}
